package com.cqan.account.controller;


import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.cqan.account.entity.Resource;
import com.cqan.account.entity.Resource.ResourceType;
import com.cqan.account.service.ResourceService;
import com.cqan.common.ViewName;
import com.cqan.controller.BaseController;
import com.cqan.exception.AppException;
import com.cqan.orm.Page;
import com.cqan.orm.PropertyFilter.MatchType;
import com.google.common.collect.Lists;

@Controller
@RequestMapping("/resource")
public class ResourceController extends BaseController<Resource,Long, ResourceService>{

	@RequestMapping(value="/save.htm",method=RequestMethod.POST)
	public  ModelAndView save(Long rid,String name,String icon
			,String url,Long pid,Integer type,Boolean enabled) throws AppException {
		Resource resource = new Resource();
		if (rid!=null&&rid!=0l) {
			resource = entityService.get(rid);
		}
		resource.setEnabled(enabled);
		resource.setCreateTime(System.currentTimeMillis());
		resource.setIcon(icon);
		resource.setName(name);
		resource.setType(ResourceType.getTypeByValue(type));
		resource.setUrl(url);
		if (pid!=null&&pid!=0) {
			Resource parent = entityService.get(pid);
			if (parent!=null) {
				resource.setParent(parent);
			}
		}
		entityService.add(resource);
		return new ModelAndView(ViewName.MAIN_HOME);
	}
	
	@RequestMapping(value="/index.htm",method=RequestMethod.GET)
	public  ModelAndView index(HttpServletRequest req, HttpServletResponse resp) throws AppException {
		
		return new ModelAndView(ViewName.RESOURCE_INDEX);
	}
	
	@RequestMapping(value="/list.htm")
	public @ResponseBody JSONObject list(HttpServletRequest req, HttpServletResponse resp) throws AppException {
		
		int pagesize = Integer.parseInt(req.getParameter("rows"));
		int pageNo = Integer.parseInt(req.getParameter("page"));
		Page<Resource> page = new Page<Resource>();
		page.setPageNo(pageNo);
		page.setPageSize(pagesize);
		page = entityService.getAllMenu(page);
		return pageToJson(page);
	}
	@RequestMapping(value="/menuList.htm")
	public @ResponseBody JSONArray menuList(HttpServletRequest req, HttpServletResponse resp) throws AppException {
		
		List<Resource> list= entityService.findBy("type", ResourceType.MENU, MatchType.EQ);
		return listToJson(list);
	}
	
	
	@RequestMapping(value="/delete.htm",method=RequestMethod.POST)
	public  ModelAndView delete(String rids) throws AppException {
		System.out.println(rids);
		if (rids!=null&&!"".equals(rids)) {
			String[] ids = rids.split("=");
			if (ids!=null&&ids.length>0) {
				List<Long> list = Lists.newArrayList();
				for (String id : ids) {
					if (id!=null&&!"".equals(id)) {
						list.add(Long.parseLong(id));
					}
				}
				if (list.size()>0) {
					entityService.delete(list);
				}
			}
		}
		return new ModelAndView(ViewName.MAIN_HOME);
	}
	
	@RequestMapping(value="/update.htm",method=RequestMethod.POST)
	public  ModelAndView update(HttpServletRequest req, HttpServletResponse resp) throws AppException {
		return null;
	}

	@Override
	@Autowired
	public void setEntityService(ResourceService entityService) {
		this.entityService = entityService;
	}
	
	
	private JSONObject pageToJson(Page<Resource> page) {
		List<Resource> reses = page.getResult();
		JSONArray arr= new JSONArray();
		for (Resource o : reses) {
			JSONObject obj = new JSONObject();
			obj.put("id", o.getId());
			obj.put("name", o.getName());
			if (o.getParent()!=null) {
				obj.put("parent", o.getParent().getName());
			}else{
				obj.put("parent","");
			}
			obj.put("icon", StringUtils.defaultIfEmpty(o.getIcon(), ""));
			obj.put("url", StringUtils.defaultIfEmpty(o.getUrl(), ""));
			obj.put("type", o.getType().getLabel());
			obj.put("enable", o.getEnabled());
			obj.put("createTime", o.getCreateTime());
			if (!arr.contains(obj)) {
				arr.add(obj);
			}
		}
		JSONObject obj = new JSONObject();
		obj.put("total", page.getTotalCount());
		obj.put("rows",arr);
		return obj;
	}
	
	/**
	 * @param list
	 * @return
	 */
	private JSONArray listToJson(List<Resource> list) {
		JSONArray arr= new JSONArray();
		for (Resource o : list) {
			JSONObject obj = new JSONObject();
			obj.put("id", o.getId());
			obj.put("name", o.getName());
			if (o.getParent()!=null) {
				obj.put("parent", o.getParent().getName());
			}else{
				obj.put("parent","");
			}
			obj.put("icon", StringUtils.defaultIfEmpty(o.getIcon(), ""));
			obj.put("url", StringUtils.defaultIfEmpty(o.getUrl(), ""));
			obj.put("type", o.getType().getLabel());
			obj.put("enable", o.getEnabled());
			obj.put("createTime", o.getCreateTime());
			if (!arr.contains(obj)) {
				arr.add(obj);
			}
		}
		return arr;
	}

}
